﻿using B.S.Data.MES.API.Applications.Management.SteamApplication.Command.RoleCommand;
using B.S.Domain.Accounts;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;

namespace B.S.Data.MES.API.Applications.Management.SteamApplication.CommandHandler.RoleCommandHandler
{
    public class RoleQueryCommandHandler : IRequestHandler<RoleQueryCommand, APIRequst<APIPageing<List<RoleEntty>>>>
    {
        public RoleQueryCommandHandler(IBaseRepository<RoleEntty> roleRepository)
        {
            RoleRepository = roleRepository;
        }

        public IBaseRepository<RoleEntty> RoleRepository { get; }

        public async Task<APIRequst<APIPageing<List<RoleEntty>>>> Handle(RoleQueryCommand request, CancellationToken cancellationToken)
        {
            var path = RoleRepository.GetAll();
            if(!string.IsNullOrEmpty(request.RoleName))
            {
                path = path.Where(a => a.RoleName.Contains(request.RoleName));
            }
            if(!string.IsNullOrEmpty(request.CreateName))
            {
                path = path.Where(a => a.CreateName.Contains(request.CreateName));
            }

            var TotalCount = path.Count();
            var pageCount = (int)Math.Ceiling(TotalCount * 1.0 / request.pageSize);
            var list=path.OrderBy(a=>a.RoleId).Skip((request.pageIndex - 1)*request.pageSize).Take(request.pageSize).ToList();

            return new APIRequst<APIPageing<List<RoleEntty>>>()
            {
                Code = RequstCode.成功,
                Msg = "查询成功",
                Data = new APIPageing<List<RoleEntty>>
                {
                    Total = pageCount,
                    TotalCount = TotalCount,
                    Data = list
                }
            };
          
            
            
        }
    }
}
